package servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.Cart;
import pojo.Customer;
import pojo.PageBean;
import service.CartServicea;

/**
 * Servlet implementation class CartServleta
 */
@WebServlet("/CartServleta")
public class CartServleta extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CartServleta() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String method=request.getParameter("method");
		switch(method) {
		case "add":
			add(request,response);
			break;
		case "showCart":
			showCart(request,response);
			break;
		}
	}

	private void showCart(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		HttpSession session=((HttpServletRequest)request).getSession();
		Customer cu=(Customer)session.getAttribute("c");
		int pageSize,currentPage;
		if(request.getParameter("pageSize")==null) {
			pageSize=5;
		}else {
			pageSize=Integer.parseInt(request.getParameter("pageSize"));
		}
		if(request.getParameter("currentPage")==null) {
			currentPage=1;
		}else {
			currentPage=Integer.parseInt(request.getParameter("currentPage"));
		}
		CartServicea cs=new CartServicea();
		PageBean<Cart> pb=cs.showCarts(currentPage, pageSize, cu.getC_name());
		request.setAttribute("pb", pb);
		try {
			request.getRequestDispatcher("cart.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpSession session=((HttpServletRequest)request).getSession();
		Customer cu=(Customer)session.getAttribute("c");
		String pid=request.getParameter("p_id");
		String quantity=request.getParameter("count");
		String price=request.getParameter("p_price");
		String image=request.getParameter("p_image");
		String pname=request.getParameter("p_name");
		Cart t =new Cart(cu.getC_name(),pid,Integer.parseInt(quantity),Float.parseFloat(price),Integer.parseInt(quantity)*Float.parseFloat(price),image,pname);
		CartServicea cs=new CartServicea();
		int flag=cs.addCart(t);
		if(flag>0) {
			try {
				response.sendRedirect("CartServleta?method=showCart");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
